﻿using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.Stock;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Stock
{
   public class OtherExWareHouseServices : IOtherExWareHouseServices
    {
        public OtherExWareHouseServices(IBaseRepository db, IOtherExWareHouseRepository or)
        {
            Or = or;
            Db = db;
        }
        public IOtherExWareHouseRepository Or { get; }
        public IBaseRepository Db { get; }

        public async Task<bool> AddAsync(OtherExWareHouse model, List<OtherExWareHouseItem> list)
        {
            var result = await Or.AddOtherExWareHouse(model, list);
            return result;
        }

        public async Task<bool> Delete(int[] Ids)
        {
            var result = await Or.DeleteOtherExWareHouse(Ids);
            return result;
        }

        public async Task<bool> Delete(int Id)
        {
            var result = await Or.DeleteOtherExWareHouse(Id);
            return result;
        }

        public async  Task<IEnumerable<OtherExWareHouse>> GetAllAsync()
        {
            string sql = "select * from OtherExWareHouse";
            var list = await Db.QueryAllAsync<OtherExWareHouse>(sql);
            return list;
        }

        public async Task<IEnumerable<OtherExWareHouse>> GetAsync(int PageIndex, int PageSize, string Number, string WareHouseId, string Status, string RealName)
        {
            string sql = "select * from OtherExWareHouse where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number";
            }
            if (!string.IsNullOrEmpty(WareHouseId))
            {
                sql += " and WareHouseId like @WareHouseId";
            }
            
            if (!string.IsNullOrEmpty(Status.ToString()))
            {
                sql += " and Status like @Status";
            }
            if (!string.IsNullOrEmpty(RealName))
            {
                sql += " and RealName like @RealName";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";
            var list = await Db.QueryAsync<OtherExWareHouse>(sql, new {PageIndex,PageSize, Number = $"%{Number}%", WarehouseId = $"%{WareHouseId}%",  Status = $"%{Status}%", RealName = $"%{RealName}%", });
            return list;
        }

       

        public async Task<int> GetCountAsync(string Number, string WarehouseId, string Status, string RealName)
        {
            string sql = "select count(*) from OtherExWareHouse where 1=1";
             if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number";
            }
            if (!string.IsNullOrEmpty(WarehouseId))
            {
                sql += " and WarehouseId like @WarehouseId";
            }
            
            if (!string.IsNullOrEmpty(Status.ToString()))
            {
                sql += " and Status like @Status";
            }
            if (!string.IsNullOrEmpty(RealName))
            {
                sql += " and RealName like @RealName";
            }
            var count = await Db.GetRecoreCountAsync (sql, new { Number = $"%{Number}%", WarehouseId = $"%{WarehouseId}%", Status = $"%{Status}%",  RealName = $"%{RealName}%", });
            return count;
        }

        public async Task<IEnumerable<OtherExWareHouseDto>> GetOtherExWareHouseItembyAsync(string OtherExWareHouseId, string WareHouseId)
        {

          
                var list = await Db.QueryAsync<OtherExWareHouseDto>(@"select ot.*,it.Spec,it.UnitOfMeasure,it.ItemCode,it.Id as ItemId,it.ItemName,it.Remark,ow.WareHouseId,
                    (isk.StockCount-isk.SalesCount) as CurrCount
                    from OtherExWareHouseItem ot
                    inner join OtherExWareHouse ow on ow.Id=ot.OtherExWareHouseId
                    inner join Item it on it.Id = ot.ItemId
                    inner join ItemStock isk on isk.ItemId = it.Id and isk.WareHouseId = @WareHouseId
                    where ot.OtherExWareHouseId =@OtherExWareHouseId", new { OtherExWareHouseId, WareHouseId });
                return list;
            

        }

        public async Task<bool> UpdateAsync(OtherExWareHouse model, List<OtherExWareHouseItem> list)
        {
            var result = await Or.UpdateOtherExWareHouse(model, list);
            return result;
        }

        public async Task<bool> UpdateStatus(long Id, int Status)
        {
            string sql = $"update OtherExWareHouse set Status=@Status where Id=@Id";
            var result = await Db.UpdateAsync(sql, new { Id, Status });
            return result;
        }
    }
}
